/*
 * ChangePw.fx
 * Author:Yu Min-ting
 * Created on 2009-7-19, 12:19:37
 */

package com.sjtu.fx.user;

import com.sjtu.fx.tools.Panel;
import javafx.scene.*;

import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import com.sjtu.fx.tools.SwingPasswordField;

import javafx.scene.control.*;

import javafx.util.Math;

import javafx.scene.control.Button;
import javafx.stage.Alert;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;

import java.lang.System;

import com.sjtu.fx.tools.UIAlert;


public class ChangePw extends CustomNode{
    var panel:Panel;

    var ppwText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Current Password: "
    };
    var ppwTextBox : SwingPasswordField = SwingPasswordField {
        translateX:210 translateY:75
        width: 150
        blocksMouse:false
        editable:true
    }

    var mpwText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* New Password: "
    };
    var mpwTextBox: SwingPasswordField  = SwingPasswordField {
        translateX:210 translateY:115
        width: 150
        blocksMouse:false
        editable:true
    }

    var cpwText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* New Password again: "
    };
    var cpwTextBox : SwingPasswordField = SwingPasswordField {
        translateX:210 translateY:155
        width: 150
        blocksMouse:false
        editable:true
    }

     //validation code
     var verifyText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Validation Code "
    };
    var verifyTextBox = TextBox {
        columns:5
        blocksMouse:false
    }
    var verifyLabel = Label {
            text: GenerateCheckCode();
            textFill:Color.ORANGERED
            font:Font {size:16 name: "Verdana Bold"}
        }

    var verifyButton = Hyperlink {
           text: "Regenerate"
            action: function() {
                verifyLabel.text = GenerateCheckCode();
                verifyTextBox.text = "";
            }
    }

    var completeInform = Label {
            visible : false
            text: "Change Successfully!"
            textFill:Color.rgb(11,51,60)
            font:Font {size:16 name: "Verdana Bold"}
            translateX:125 translateY:350
        }

    var required = Text {
             content: "* = Required"
             font: Font {size:11 name: "Verdana Bold"}
             fill: Color.RED
             translateX:50
             translateY:290
         }
      var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
     var completeButton = Button {
             text: "Complete"
             translateX:180 translateY:320
             action: function() {
                //not input the full textboxes
                var passWd:String = (this.parent.lookup("loginPanel") as Login).password;
                if(ppwTextBox.text==""or mpwTextBox.text==""
                     or cpwTextBox.text=="" or cpwTextBox.text != mpwTextBox.text)
                {
                    alert.visible = true;
                    //Alert.inform("Eooro","Please input entire information");
                }else{
                      if(ppwTextBox.text != passWd){
                          alert.content ="old Password Input Error!";
                          alert.visible = true;
                      }

                            //Alert.inform("ERROR","old Password Input Error!");
                      if(verifyTextBox.text!=verifyLabel.text)
                         {
                             alert.content = "Wrong validation code!\n Please input again.";
                             alert.visible = true;
                           // Alert.inform("ERROR","Wrong validation code!\n Please input again.");
                            //regenerate code
                            verifyLabel.text=GenerateCheckCode();
                            verifyTextBox.text = "";
                         }
                         else{
                            var userid:Integer = (this.parent.lookup("loginPanel") as Login).userid;
                            var newPassWd:String = cpwTextBox.text;
                            ChangePassWd(userid,passWd,newPassWd);
                            verifyTextBox.text="";
                            ppwTextBox.text="";
                            cpwTextBox.text="";
                            mpwTextBox.text="";

                         }
                 }
            }
    }

     override protected function create() : Node {
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Change Password"
                    translateX: 0
                    translateY: 0
                },

                VBox{
                    spacing: 30
                    translateX:50 translateY: 80
                    content:[ppwText, mpwText, cpwText]
                },
                ppwTextBox,mpwTextBox,cpwTextBox,
                 HBox {
                    spacing: 20
                    translateX:50 translateY: 220
                    content:[verifyText,verifyTextBox,verifyLabel,verifyButton]
                },completeInform, required, completeButton,alert

            ]
        }
     }

//generate check code
 function GenerateCheckCode():String
 {
    //initialize
    var ran:Number;
    var number:String;
    var checkCode:String ="";
     //Math.random�
     ran=Math.random();
     number =ran.toString();
     checkCode = number.substring(2, 6);
    return checkCode;
 }
 function ChangePassWd(userid:Integer,oldPass:String,passWd:String){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/ChangePass"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("id",userid);
                json.put("password", oldPass);
                json.put("newPassword",passWd);
                System.out.println(passWd);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                if(result.equals("true")){
                completeInform.visible = true;
                }else{
                    alert.content = "something happen to the server.";
                    alert.visible = true;
                    //Alert.inform("something happen to the server.");
                }
            } finally {
                is.close();
            }
        }
    }.start();
 }


}
